package com.wxd.woff.graphics;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wxd.woff.utils.BoundingBox;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: wangxindong
 * @date: 2018/9/6-19:41
 */
public class SampledImageReader extends AbstractWoffImage implements WoffImage {

    private static final String FILE_EXTEND = ".+(.JPEG|.jpeg|.JPG|.jpg|.GIF|.gif|.BMP|.bmp|.PNG|.png)$";
    private static final Pattern FILE_EXTEND_NAME = Pattern.compile(FILE_EXTEND);

    private TTFGraphicsPathData graphicsPathData;
    private String fileName;

    private BoundingBox fontBox;

    public SampledImageReader() {

    }

    public SampledImageReader(TTFGraphicsPathData graphicsPathData) {
        this.graphicsPathData = graphicsPathData;
        if (null != graphicsPathData) {
            this.fontBox = graphicsPathData.getFontBox();
        }
    }

    /**
     * 获取文件扩展名成
     */
    private String getFileExtendName(String name) {
        Matcher m = FILE_EXTEND_NAME.matcher(name);
        if (m.find()) {
            String value = m.group(1);
            return value;
        }
        return null;
    }

    @Override
    public void write(BufferedImage image, String formatName, OutputStream out) throws IOException {
        OutputStream output = new ByteArrayOutputStream();
        ImageIO.write(image, formatName, output);
    }

    @Override
    public void write(BufferedImage image, String formatName, String fileName) throws IOException {
        String name = getFileExtendName(fileName);
        if (null == name) {
            LOGGER.error("fileName is not validate {}", fileName);
            throw new RuntimeException("fileName is not validate");
        }
        Graphics2D graphics2D = image.createGraphics();
        graphics2D.setColor(Color.BLACK);
        graphics2D.fillRect(0, 0, getWidth(), getHeight());
        graphics2D.setColor(Color.WHITE);
        double[] matrix = new double[]{1, 0, 0, -1, 0, getHeight()};
        AffineTransform affineTransform = new AffineTransform(matrix);
        graphics2D.setTransform(affineTransform);
        graphics2D.fill(graphicsPathData.getPath());
        ImageIO.write(image, formatName, new File(fileName));
    }

    @Override
    public boolean isEmpty() {
        return graphicsPathData == null;
    }

    @Override
    public int getHeight() {
        return fontBox.getUpperRightY();
    }

    @Override
    public int getWidth() {
        return fontBox.getUpperRightX();
    }

    /**
     * 获取文件后缀名
     *
     * @return
     */
    @Override
    public String getSuffix() {
        return null;
    }

    public TTFGraphicsPathData getGraphicsPathData() {
        return graphicsPathData;
    }

    public void setGraphicsPathData(TTFGraphicsPathData graphicsPathData) {
        this.graphicsPathData = graphicsPathData;
        if (null != graphicsPathData) {
            this.fontBox = getFontBox();
        }
    }

    public BoundingBox getFontBox() {
        if (!isEmpty()) {
            return graphicsPathData.getFontBox();
        }
        return null;
    }
}
